package com.homework;

import java.util.Calendar;
import java.util.Scanner;

/**
 * @author xiyu
 * @version 1.0
 * @date 2025/3/29 21:23
 * @description
 */
public class T7 {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入年份: ");
        int year = scanner.nextInt();
        System.out.print("请输入月份: ");
        int month = scanner.nextInt();

        inputCalendar(year, month);
    }

    public static void inputCalendar(int year, int month) {
        // 获取当前日期
        Calendar currentDate = Calendar.getInstance();
        int nowyear = currentDate.get(Calendar.YEAR);
        int nowmonth = currentDate.get(Calendar.MONTH) + 1;
        int nowday = currentDate.get(Calendar.DAY_OF_MONTH);
        // 创建日历
        Calendar calendar = Calendar.getInstance();
        calendar.set(year, month - 1, 1);
        // 获取第一天是星期几
        int firstDayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
        // 获取天数
        int daysInMonth = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
        //空格
        System.out.println("一\t二\t三\t四\t五\t六\t日");
        int dayOfWeek = firstDayOfWeek;
        if (dayOfWeek == 1) {
            dayOfWeek = 7;
        } else {
            dayOfWeek--;
        }
        for (int i = 1; i < dayOfWeek; i++) {
            System.out.print("\t");
        }
        //日历
        for (int day = 1; day <= daysInMonth; day++) {
            boolean isToday = (year == nowyear && month == nowmonth && day == nowday);
            if (dayOfWeek == 6 || dayOfWeek == 7) {
                if (isToday) {
                    System.out.print("[*" + day + "]");
                } else {
                    System.out.print("[" + day + "]");
                }
            } else {
                if (isToday) {
                    System.out.print("*" + day);
                } else {
                    System.out.print(day);
                }
            }
            System.out.print("\t");
            if (dayOfWeek == 7) {
                System.out.println();
                dayOfWeek = 1;
            } else {
                dayOfWeek++;
            }
        }
    }
}
